#pragma once

#include <iostream>
#include <string>
#include <cstdlib>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#include "Log.hpp"
#include "Mutex.hpp"
#include "Common.hpp"
#include "InetAddr.hpp"
const int gdefaultsockfd = -1;
const int gbacklog = 8;
namespace SocketModule
{
    using namespace LogModule;
    class Socket; // 前置声明
    using SockPtr = std::shared_ptr<Socket>;
    class Socket // 基类,只负责每个子类要实现的方法,不负责具体实现
    {
    public:
        virtual ~Socket() = default;
        // 创建
        virtual void SocketOrDie() = 0;
        // 地址复用选项
        virtual void SetSocketOpt() = 0;
        // 绑定
        virtual void BindOrDie(uint16_t port) = 0;
        // 监听
        virtual void ListenOrDie() = 0;
        // 连接
        virtual int Accepter(InetAddr *client) = 0;
        // 关闭
        virtual void Close() = 0;
        // 发送消息
        virtual int Send(const std::string &out) = 0;
        // 接收消息
        virtual int Recv(std::string *in) = 0;
        // 返回fd的值
        virtual int Fd() = 0;
        // 提供一个创建ListenSocket的固定方法
        void BuildTcpSocketMethod(uint16_t port)
        {
            // 创建套接字
            SocketOrDie();
            // 地址复用快速重连
            SetSocketOpt();
            // 填充网络信息并绑定
            BindOrDie(port);
            // 设置为监听状态
            ListenOrDie();
        }
    };
    class TcpSocket : public Socket // 子类负责实现基类方法的具体细节
    {
    public:
        TcpSocket() // 监听套接字用的构造
            : _sockfd(gdefaultsockfd)
        {
        }
        TcpSocket(int sockfd) // 通信套接字用的构造
            : _sockfd(sockfd)
        {
        }
        virtual ~TcpSocket()
        {
        }
        // 创建
        virtual void SocketOrDie() override
        {
            _sockfd = socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(Loglevel::FATAL) << "socket filed";
                Die(SOCKET_ERROR);
            }
            LOG(Loglevel::DEBUG) << "socket success,sockfd:" << _sockfd;
        }
        // 地址复用选项
        virtual void SetSocketOpt() override
        {
            int opt = 1;
            setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
        }
        // 绑定
        virtual void BindOrDie(uint16_t port) override
        {
            struct sockaddr_in local;
            memset(&local, 0, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_port = htons(port);
            local.sin_addr.s_addr = INADDR_ANY;
            int n = bind(_sockfd, CONV(&local), sizeof(local));
            if (n < 0)
            {
                LOG(Loglevel::FATAL) << "bind filed";
                Die(BIND_ERROR);
            }
            LOG(Loglevel::DEBUG) << "bind success,socket:" << _sockfd;
        }
        // 监听
        virtual void ListenOrDie() override
        {
            int n = listen(_sockfd, gbacklog);
            if (n < 0)
            {
                LOG(Loglevel::FATAL) << "listen filed";
                Die(LISTEN_ERROR);
            }
            LOG(Loglevel::DEBUG) << "listen success,socket:" << _sockfd;
        }
        // 连接
        // 1.拿到文件描述符 2.获取客户端信息
        virtual int Accepter(InetAddr *client) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int newsockfd = accept(_sockfd, CONV(&peer), &len); // accept之后客户端信息保存在peer中,要将peer的信息拷贝到client中
            if (newsockfd < 0)
            {
                LOG(Loglevel::WARING) << "accept filed";
                return -1;
            }
            client->SetAddr(peer, len);
            return newsockfd;
        }
        // 关闭
        virtual void Close() override
        {
            if (_sockfd == gdefaultsockfd)
            {
                return;
            }
            close(_sockfd);
        }
        // 发送消息,这里是通信套接字的方法
        virtual int Send(const std::string &in) override
        {
            int n = send(_sockfd, in.c_str(), in.size(), 0);
            return n;
        }
        // 接收消息,也是通信套接字的方法
        virtual int Recv(std::string *out) override
        {
            char inbuffer[1024 * 8];
            int n = recv(_sockfd, inbuffer, sizeof(inbuffer), 0);
            if (n > 0)
            {
                inbuffer[n] = 0;
                *out = inbuffer;
            }
            return n;
        }
        // 返回fd的值
        virtual int Fd() override
        {
            return _sockfd;
        }

    private:
        int _sockfd;
    };
}